﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.Extensions/ModelStateExtensions 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       ModelStateExtensions
* 创建时间：  2025/6/3 17:21:54
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.Extensions
{
    public static class ModelStateExtensions
    {
        public static string GetValidationSummary(this ModelStateDictionary modelState, string separator = "\r\n")
        {
            if (modelState.IsValid)
            {
                return null;
            }

            StringBuilder stringBuilder = new StringBuilder();
            foreach (KeyValuePair<string, ModelStateEntry> item in modelState)
            {
                ModelStateEntry value = item.Value;
                string value2 = value.Errors.FirstOrDefault((ModelError p) => !string.IsNullOrWhiteSpace(p.ErrorMessage))?.ErrorMessage;
                if (string.IsNullOrWhiteSpace(value2))
                {
                    value2 = value.Errors.FirstOrDefault((ModelError o) => o.Exception != null)?.Exception.Message;
                }

                if (!string.IsNullOrWhiteSpace(value2))
                {
                    stringBuilder.Append(value2);
                    break;
                }
            }

            return stringBuilder.ToString();
        }
    }
}
